const UserModel = require("../model/UserModel")

const UserService = {
    getList:async () => {
        //联系role集合查询
        return await UserModel.find().populate("role")
    },
    login:async (body) => {
        return await UserModel.find(body).populate("role")
    },
    addList:async (body) => {
        return await UserModel.create(body)
    },
    deleteList:async(id) => {
        return await UserModel.findByIdAndDelete(id)
    },
    updateList:async(id,body) => {
        return await UserModel.findByIdAndUpdate(id,body)
    },
    upload:async (id,file,body) => {
        if(file){
            return await UserModel.findByIdAndUpdate(id,{
                ...body,
                username:body.username,
                avatarUrl:`/uploads/${file.filename}`
            },{returnDocument:"after"/*返回更新后的文档*/}).find().populate("role")
        }else{
            return await UserModel.findByIdAndUpdate(id,{
                ...body,
                username:body.username,
                // avatarUrl:`/uploads/${file.filename}`
            },{returnDocument:"after"/*返回更新后的文档*/}).find().populate("role")
        }
    }
}
module.exports = UserService